//
//  HFMainTabBar.swift
//  sinaMicroBlog
//
//  Created by mac on 15/11/8.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

class HFMainTabBar: UITabBar {
    
    //通过重写init来进行界面加载
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        setUI()
    }
    
    
    private func setUI() {
        addSubview(composeButton)
    }
    
    //懒加载一个撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: .Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: .Highlighted)
        button.sizeToFit()
        
        return button
    }()
    
    //更改原来按钮的属性,安放撰写按钮
    override func layoutSubviews() {
        super.layoutSubviews()
        let w = bounds.width / 5
        let h = bounds.height
        let rect = CGRect(x: 0, y: 0, width: w, height: h)
        
        var index:CGFloat = 0
        for subview in subviews {
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                subview.frame = CGRectOffset(rect, w * index, 0)
                
                index += index == 1 ? 2 : 1
            }
        }
        
        composeButton.frame = CGRectOffset(rect, w * 2, -20)
        bringSubviewToFront(composeButton)
        
    }
    
}
